package com.activity.fragment;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.api.viewModel.LoginVM;
import com.example.apple.ddhr_new.R;
import com.example.lzbase.BaseViewModelFragment;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhefei.view.largeimage.LargeImageView;
import com.shizhefei.view.largeimage.factory.InputStreamBitmapDecoderFactory;

import java.io.IOException;

import butterknife.BindView;

import static android.content.Context.WINDOW_SERVICE;

public class ShopFragment extends BaseViewModelFragment<LoginVM> {

    @BindView(R.id.iv_icon)
    LargeImageView ivIcon;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_shop;
    }

    @Override
    public void initStatusBar() {
        ImmersionBar.with(this)
                .titleBar(rootView.findViewById(R.id.nav_bar))
                .statusBarDarkFont(true)
                .flymeOSStatusBarFontColor(R.color.black)
                .init();
    }

    @Override
    public void createView() {
        super.createView();
        LargeImageView imageView= ivIcon;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        WindowManager m = ((Activity) mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        params.height = (int) (d.getWidth()*(3500.0/1242.0));
        imageView.setLayoutParams(params);
        try {
            ivIcon.setImage(new InputStreamBitmapDecoderFactory(getResources().getAssets().open("shop.png")) );
        } catch (IOException e) {
            e.printStackTrace();
        }

//        WindowManager systemService = (WindowManager) ((Activity)(mContext)).getSystemService(WINDOW_SERVICE);
//        Point point = new Point();
//        systemService.getDefaultDisplay().getSize(point);
//        ivIcon.setImageBitmap(decodeSampledBitmapFromResource(this.getResources(),R.drawable.shop, point.x, point.y));
    }


    /***
     *  加载缩小比例的图片
     * @param res
     * @param resId
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            inSampleSize *= 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
}
